package com.ptrsystem.siatd.dao.implementacion;

import com.ptrsystem.siatd.beans.DocumentoIngresoExt;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class MantenimientoDocumentoIngresoExtImplement implements com.ptrsystem.siatd.dao.interfaces.MantenimientoDocumentoIngresoExtInterface {

    @Autowired
    private DataSource dataSource;

    @Override
    @Transactional
    public boolean getRegistraDocIngExt(DocumentoIngresoExt docIngExt) {
        boolean rt = false;
        JdbcTemplate jdbc = new JdbcTemplate(this.dataSource);
        int rs = jdbc.update("insert into TD_DOC_INGRESO_EXT values(?,?,?,?,?,?,?,?,?,?,?,?)",
                docIngExt.getCodTipoDocIngExt(),
                docIngExt.getCodAnio(),
                docIngExt.getNroTipDocIngExt(),
                docIngExt.getNomTipDocIngExt(),
                docIngExt.getAsunto(),
                docIngExt.getFecha(),
                docIngExt.getUsuDestDocIngExt(),
                docIngExt.getRutaDocIngExt(),
                docIngExt.getCodEstDocIngExt(),
                docIngExt.getCodTipoUsuExt(),
                docIngExt.getDniUsuario(),
                docIngExt.getCodEstDocExt());

        if (rs > 0) {
            rt = !rt;
        }
        return rt;
    }

    @Override
    public int getGeneraNroDocIngExt(String dni) {
        String rs = "0";
        JdbcTemplate jdbc = new JdbcTemplate(this.dataSource);
        rs = jdbc.queryForObject("select convert(int,(MAX(NRO_TIPO_DOC_ING_EXT ))) from TD_DOC_INGRESO_EXT where DNI_USUARIO=?",new Object[]{dni}, String.class);
        if (rs == null) {
            rs = "0";
        }

        int rt = Integer.parseInt(rs);
        rt += 1;
        return rt;
    }

    @Override
    public int getGeneraCodDocIngExt() {
        String rs = "0";
        JdbcTemplate jdbc = new JdbcTemplate(this.dataSource);
        rs = jdbc.queryForObject("select convert(int,(MAX(COD_TIPO_DOC_ING_EXT ))) from TD_DOC_INGRESO_EXT", String.class);
        if (rs == null) {
            rs = "0";
        }
        int rt = Integer.parseInt(rs);
        rt += 1;
        return rt;
    }

}
